class NumberDataset(Dataset):
      def __init__(self,path,type_transform):
        super(NumberDataset,self).__init__()
        self.path = path
        # 加载数据集图片列表
        self.picture_list = self.load_image_list()
        # 数据转换
        self.transform = type_transform
        # 数字字符映射表
        self.map=["1","2","3","4","5","6","7","8","9"]
    def load_image_list(self):
        """
        计算数据集的长度 
        """
        # 获取指定路径下的所有文件名
        list_data = list(os.walk(self.path))[0][-1]
        return list_data
    def __len__(self):
        # 返回数据集的长度
        return len(self.picture_list)
    def __getitem__(self, item):
        # 打开图片
        image = Image.open(os.path.join(self.path,self.picture_list[item])) 
        if self.transform:
            # 转换图像格式为Tensor
            image = self.transform(image)
        # 获取该图像对应的数字字符
        labels=[self.map.index(i) 
            for i in self.picture_list[item].split("_")[0]] 
        # 将数字字符转换为Tensor类型的标签
        labels = torch.as_tensor(labels, dtype=torch.int64)
        return image, labels 
